import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/utils/date_utils.dart';
import 'package:get/get.dart';

import '../login/kuangdeng/kuang_deng_login2_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///个人信息页面
class PersionInfoPage extends StatefulWidget {
  ///页面索引 0 为个人中心进入  1为首页面侧拉
  final int pageIndex;

  const PersionInfoPage({Key key, this.pageIndex = 0}) : super(key: key);

  @override
  _PersionInfoPageState createState() => _PersionInfoPageState();
}

class _PersionInfoPageState extends State<PersionInfoPage> {
  UserBean _userBean;
  MineBean _mineBean;

  @override
  void initState() {
    super.initState();
    _userBean = UserHelper.getInstance.userBean;
    _mineBean = UserHelper.getInstance.mineBean;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("个人信息"),
        centerTitle: true,
        elevation: 1.0,
      ),

      ///填充布局
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.all(8),
                  margin: EdgeInsets.only(bottom: 10),
                  color: Colors.white,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      buildContainer(
                          _userBean.department == "中台学习" ? "昵称" : "姓名",
                          _userBean.reamName),
                      Divider(),
                      buildContainer("所在矿", _mineBean.name),
                      Divider(),
                      buildContainer("部门", _userBean.department),
                      Divider(),
                      buildContainer("职位", _userBean.userPost),
                    ],
                  ),
                ),
                buildOther(),
                buildExtiFunction(),
              ],
            ),
          )),
    );
  }

  Container buildContainer(String leftText, String rightText) {
    if (rightText == null) {
      rightText = "--";
    }
    return Container(
      height: 33,
      child: Row(
        children: [Expanded(child: Text("$leftText")), Text("$rightText")],
      ),
    );
  }

  buildOther() {
    if (_userBean.department == "中台学习") {
      return Container(
        padding: EdgeInsets.all(8),
        color: Colors.white,
        margin: EdgeInsets.only(bottom: 10),
        child: buildContainer("账号", _userBean.mobile),
      );
    }
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildContainer("账号", _userBean.username),
              Divider(),
              buildContainer("员工号", _userBean.staffNum),
              Divider(),
              buildContainer("手机号", _userBean.mobile),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildContainer("身份证号", _userBean.idCard),
              Divider(),
              buildContainer("学历", _userBean.educationalBackground),
              Divider(),
              buildContainer("职称", _userBean.technicalTitle),
              Divider(),
              buildContainer("政治面貌", _userBean.politicsStatus),
              Divider(),
              buildContainer(
                  "入党时间", ViDateUtils.formatDateStr(_userBean.factionTime)),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildContainer(
                  "参加工作时间", ViDateUtils.formatDateStr(_userBean.entryTime)),
              Divider(),
              buildContainer("籍贯", _userBean.userAddress),
              Divider(),
              buildContainer(
                  "生日", ViDateUtils.formatDateStr(_userBean.birthday)),
              Divider(),
              buildContainer("个性签名", _userBean.autograph),
            ],
          ),
        )
      ],
    );
  }

  buildExtiFunction() {
    if (widget.pageIndex != 1) {
      return Container();
    }
    return Container(
      margin: EdgeInsets.only(top: 30),
      child: ElevatedButton(
        onPressed: () {
          //清除缓存用户信息
          UserHelper.getInstance.exitLogin();
          //关闭首页面
          // rootStreamController.add(GlobalBean(202, "关闭"));
          // NavigatorUtils.pop(context);
          Get.offAll(() => KuangDengLogin2Page());
        },
        child: Text("退出登录"),
      ),
    );
  }
}
